package com.boot.config;

import com.boot.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Autowired
    LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathPatterns = new ArrayList<>();
        excludePathPatterns.add("/");
        excludePathPatterns.add("/user/login");
        excludePathPatterns.add("/*.js");
        excludePathPatterns.add("/*.css");
        excludePathPatterns.add("/*.jpeg");
        excludePathPatterns.add("/user/doLogin");
        excludePathPatterns.add("/user/doRegister");

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") //拦截所有请求，包括静态资源文件
                .excludePathPatterns(excludePathPatterns); //放行登录页，登陆操作，静态资源
    }
}
